#! /bin/csh -f
#  Perform system33 query, results as to hypertext
#
#  /sys33/doc/<id>				Retrieve document
#        /desc/<id>				Retrieve descriptor
#        /area/<areaname>/field/<fieldname>/	Simple search
#                        /full			Full command search
set noglob
set line = "$<"
set line = ($line)		# Break into words
set w3id = $line[2]		# Pick up second arg
if ("$w3id" =~ *\?*) then
    echo you asked for a query of $w3id
    set halves = `echo $w3id | tr '?' ' ' `      # Split keywords from index name
    set index = `echo $halves[1] | tr '/' ' ' `  # Split index name by slashes
    set words = `echo $halves[2] | tr '+' ' ' `  # Split keywords by plusses
    set area = $index[3]
    set searchtype = $index[4]

# all other things being OK, ...
    set fieldname = $index[5]
    set query = ()
    foreach w ($words)
	set query = ( $query equal $fieldname $w )
    end
    echo "<title>$words in $index</title>"
    echo "<h1>$words</h1>"
    echo "The following documents match:"
    echo "<ul>"
    system33client -q -N -S/net/%s -F%s -p descriptionsearch $area $query | xargs -l awk -f d2p.awk	
    echo "</ul>"

else       # Not search
    echo you asked to retrieve $w3id
    set parts = `echo $w3id | tr '/' ' ' `    # Break at slashes into array
    if ($parts[1] != "sys33") goto error
    if ($parts[2] == "doc") then		# Document itself	docid = $parts[3]
	set filename = `system33client -q -N -S/net/%s -F"%s " -A%s -p retrieve -f html text -d $parts[3]`
	if ($#filename == 0) then
		echo "Sorry no filename!"
		exit
	endif
	if ($filename[2] != "html") echo "<PLAINTEXT>"
	cat $filename[1]
	exit

    else if ($parts[2] == "desc") then
	set filename = `system33client -q -N -S/net/%s -F"%s " -A%s -p retrieve -f desc -d $parts[3]`
	if ($#filename == 0) then
		echo "Sorry no filename!"
		exit
	endif
	echo "<h1>Description of document </h1>"
	if ($filename[2] != "html") echo "<PLAINTEXT>"
	cat $filename[1]
	exit
    else if ($parts[2] == "area") then

    endif
endif		# Not search
exit

error:  echo "Bad document id syntax: $w3id"
exit
